<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class CommandShowUserWallet extends Command
{
    protected $signature = 'CommandShowUserWallet';

    protected $description = '查询用户剩余账户余额';

    public function __construct()
    {
        parent::__construct();
    }

    public function handle()
    {        
        do {
            $userList = DB::table('finance_wallet as a')
                ->leftJoin('user as b', "a.user_id", "=", "b.user_id")
                ->select(['a.user_id','b.uuid','b.nickname','b.mobile_phone','a.diamond_balance','a.kk_balance'])
                ->get();
            $userList = objToArray($userList);

            //背包礼物
            $res4 = DB::table('gift_bag as a')
                ->leftJoin('gift_library as b' ,'a.gift_id','=','b.gift_id')
                ->where('a.avail', '>', 0)
                ->selectRaw('user_id, sum(avail*price) as bag_diamond_num')
                ->groupBy('a.user_id')
                ->get();
            $res4 = objToArray($res4);
            $res4 = array_column($res4, null, 'user_id');

            //公会主播
            $unionList = DB::table('union_user as a')
                ->leftJoin('union as b', "a.un_id", "=", "b.id")
                ->where('b.stop', 1)
                ->where('b.authentication_status', '>', 0)
                ->select(['a.user_id','b.id','b.name'])
                ->get();
            $unionList = array_column(objToArray($unionList), null, 'user_id');

            foreach($userList as $key=>$val) {
                $val['union_id'] = empty($unionList[$val['user_id']]['id']) ? '' : $unionList[$val['user_id']]['id'];
                $val['union_name'] = empty($unionList[$val['user_id']]['name']) ? '' : $unionList[$val['user_id']]['name'];

                $val['bag_diamond_num'] = empty($res4[$val['user_id']]['bag_diamond_num']) ? 0 : $res4[$val['user_id']]['bag_diamond_num'];

                if($val['diamond_balance']==0 && $val['kk_balance']==0 && $val['bag_diamond_num']==0) {
                    continue;
                }

                $id = DB::table('user_wallet_temp')->insertGetId($val);
                echo $id."\r\n";
            }

        } while (false);

        return true;
    }


}